package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 消息adapter
 */
public class DirverHomeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DIrverOrderBean> dirverHomeBeanList;
    private Context context;
    private RecyclerViewItemClickLisenter<DIrverOrderBean> onItemClickLisenter;
    private DirverHomeListJieDanCallback dirverHomeListJieDanCallback;
    java.text.DecimalFormat df =new java.text.DecimalFormat("#.###");

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnItemClickLisenter(RecyclerViewItemClickLisenter<DIrverOrderBean> onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    public void setDirverHomeListJieDanCallback(DirverHomeListJieDanCallback dirverHomeListJieDanCallback) {
        this.dirverHomeListJieDanCallback = dirverHomeListJieDanCallback;
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DIrverOrderBean dirverHomeBean= (DIrverOrderBean) view.getTag();
            if (view.getId()==R.id.tv_caozuo){
                dirverHomeListJieDanCallback.jiedan(dirverHomeBean);
            }else {
                onItemClickLisenter.onItemClickLisenter(dirverHomeBean);
            }
        }
    };

    public DirverHomeListAdapter(List<DIrverOrderBean> dirverHomeBeanList, Context context) {
        this.dirverHomeBeanList = dirverHomeBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_dirver_home,null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder= (MyHolder) holder;
        DIrverOrderBean dirverHomeBean=dirverHomeBeanList.get(position);
        myHolder.tvStart.setText(dirverHomeBean.getBeginDate()+"-"+dirverHomeBean.getEndDate());
        myHolder.tvType.setText(dirverHomeBean.getMeshTypeName());
        myHolder.tvNumber.setText(dirverHomeBean.getMeshNum());
        myHolder.tvTime.setText(dirverHomeBean.getCreateTime());
        myHolder.tvPhone.setText(dirverHomeBean.getMeshUserPhone());
        myHolder.tvMoney.setText(dirverHomeBean.getWorkPrice()+"");
        myHolder.tvAddress.setText(dirverHomeBean.getWorkAddress());
        if (TextUtils.isEmpty(dirverHomeBean.getDistance())||"null".equals(dirverHomeBean.getDistance())){
            LatLng latLng2=new LatLng(dirverHomeBean.getMeshLat(),dirverHomeBean.getMeshLng());
            LatLng latLng=new LatLng(LocationManager.getInstance().getLatitude(),LocationManager.getInstance().getLongitude());
            try {
                String ds=df.format(AMapUtils.calculateLineDistance(latLng,latLng2)/1000);
                if (ds.contains("-")){
                    myHolder.tvJl.setText("0.01KM");

                }else {
                    myHolder.tvJl.setText(ds+"KM");
                }
            }catch (Exception e){
                myHolder.tvJl.setText("0.01KM");

            }

        }else {
            myHolder.tvJl.setText(dirverHomeBean.getDistance()+"KM");
        }
        myHolder.tvWorkType.setText(dirverHomeBean.getWorkTypeName());
        myHolder.tvCz.setTag(dirverHomeBean);
        myHolder.tvCz.setOnClickListener(onClickListener);
        if (0==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("已取消");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
        }else if (1==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("立即接单");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
        }else if (2==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("待移交");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_dd));
        }else if (3==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("确认");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_3));
        }else if (4==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("使用中");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
        }else if (5==dirverHomeBean.getOrderStatus()){
            myHolder.tvCz.setText("已完成");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
        }
        myHolder.itemView.setTag(dirverHomeBean);
        myHolder.itemView.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return dirverHomeBeanList.size();
    }
    public class MyHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_start_tv)
        TextView tvStart;
        @BindView(R.id.item_number_tv)
        TextView tvNumber;
        @BindView(R.id.item_type_tv)
        TextView tvType;
        @BindView(R.id.item_juli_tv)
        TextView tvJl;
        @BindView(R.id.item_time_tv)
        TextView tvTime;
        @BindView(R.id.item_phone_tv)
        TextView tvPhone;
        @BindView(R.id.item_address_tv)
        TextView tvAddress;
        @BindView(R.id.item_money_tv)
        TextView tvMoney;
        @BindView(R.id.tv_caozuo)
        TextView tvCz;
        @BindView(R.id.item_worktype_tv)
        TextView tvWorkType;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface  DirverHomeListJieDanCallback{
        void  jiedan(DIrverOrderBean dirverHomeBean);
    }

}
